package cn.sail.shop3.api.service.impl;

import cn.sail.shop3.api.dao.UserDao;
import cn.sail.shop3.api.dao.impl.UserDaoImpl;
import cn.sail.shop3.api.entity.User;
import cn.sail.shop3.api.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User checkLogin(String userName, String password) {
        //根据用户名查询用户
        User dbUser = userDao.selectByUserName(userName);

        if (userName == "" && password == "") {
            //用户和密码都不能为空，直接返回
            throw new RuntimeException("用户和密码都不能为空");
        }
        if (userName == "") {
            //用户不能为空，直接返回
            throw new RuntimeException("用户不能为空");
        }
        if (null == dbUser) {
            //用户不存在，直接返回
            throw new RuntimeException("用户不存在");
        }

        //用户存在，密码判断
        if (password == "") {
            //密码不能为空
            throw new RuntimeException("密码不能为空");
        }
        //用户存在，密码判断
        if (!dbUser.getPassword().equals(password)) {
            //密码错误
            throw new RuntimeException("密码错误");
        }
        //密码正确
        return dbUser;
    }

    @Override
    public int insert(String userName, String password) throws RuntimeException {
        return 0;
    }
}
